<?php
/**
 * User: lee x q
 * Date: 2018/6/27
 * Time: 10:45
 */
class publicControl extends  Control
{

    public $redis;

    public $deploy;

    public function __construct()
    {
        $this->redis = new Redis();
        $this->deploy = C('redis');
        $this->redis->connect($this->deploy['master']['host'],$this->deploy['master']['port']);
        $this->_check_sign();
    }

    /**
     * 商城(内部)首页频道接口
     */
    public function categorysOp()
    {
        /* @var ticket_categoryModel $cate_model */
        $cate_model = Model('ticket_category');
        $cates = $cate_model->getCategoryList(array('is_del'=>0,'tc_pid'=>array('gt','0')));
        $cates_all = array();
        foreach ($cates as $k => $cate) {
            if($cate['tc_pid']==1){
                $cate['url'] = BASE_URL.'?act=hotel&op=index&cate_id='.$cate['tc_id'];
                //酒店
                $cates_all[0]['url'] = BASE_URL.'?act=hotel';
                $cates_all[0]['Subclass'][]= $cate;
            }elseif($cate['tc_pid']==4){
                //旅行社
                $cate['url'] = BASE_URL.'?act=itinerary&op=index&cate_id='.$cate['tc_id'];
                $cates_all[1]['url'] = BASE_URL.'?act=itinerary';
                $cates_all[1]['Subclass'][] = $cate;

            }elseif($cate['tc_pid']==2){
                //景区
                $cate['url'] = BASE_URL.'?act=ticket&op=index&cate_id='.$cate['tc_id'];
                $cates_all[1]['url'] = BASE_URL.'?act=ticket';
                $cates_all[2]['Subclass'][] = $cate;

            }elseif($cate['tc_pid']==3){
                //美食
                $cate['url'] = BASE_URL.'?act=food&op=index&cate_id='.$cate['tc_id'];
                $cates_all[3]['url'] = BASE_URL.'?act=food';
                $cates_all[3]['Subclass'][] = $cate;

            }
        }
        $cates_all[2]['url'] = BASE_URL.'?act=ticket';
        $this->msgbox('商城频道分类',array('list'=>array_values($cates_all)),2000);

    }

    /**
     * 根据经纬度推送消息
     */
    public function get_shop_infoOp()
    {
        //$lng = 24.471;//经度
        //$lat = 118.17432;//纬度
        //$device_id = '5620D8AA6D234E93B84BBA96B3ED870D';
        $device_id = getgpc('device_id');//设备ID
        $lng = getgpc('lng');//经度
        $lat = getgpc('lat');//纬度
        /* @var ticketModel $ticket_model  */
        $ticket_model = Model('ticket');
        $condition = array();
        $condition['tk_status'] = 1;
        $distance = 'SQRT(pow('. $lng .' - tk_lat, 2) + pow('. $lat .'  - tk_lng, 2)) AS distance';
        $field = '*';
        $field .=','.$distance;
        $order = 'distance asc';
        if($lng && $lat && $device_id)
        {
            $list = $ticket_model->getTicketListPage($condition,$field,1,$order);//只取距离最近的一条记录

            if($list && is_array($list))
            {
                $distance = round($list[0]['distance'] * 10000) / 100;

                if($distance<=5)
                {
                    //查询该条记录是否过期
                    if(!$this->redis->get($this->deploy['prefix'].'ticket_push-'.$device_id.'-'.$list[0]['tk_id'])){
                        //小于5km推送
                        $title = $list[0]['tk_title'];
                        $url = BASE_URL.'?act=ticket&op=ticket_info&tk_id='.$list[0]['tk_id'];
                        $res = $this->httpPost(PUSH_URL,array('title'=>$title,'url'=>$url,'device_id'=>$device_id));
                        $res = object_array(json_decode($res));
                        if($res['code']==2000)
                        {
                            //写入缓存默认24小时后过期
                            $this->redis->set($this->deploy['prefix'].'ticket_push-'.$device_id.'-'.$list[0]['tk_id'],'ing',86400);
                            $this->msgbox('推送成功',array('list'=>[]),2000);
                        }else
                        {
                            $this->msgbox('推送失败',array('list'=>[]),2000);
                        }

                    }

                }else
                {
                    $this->msgbox('调用成功,附近没有景区',array('list'=>[]),2000);
                }
            }
        }
        $this->msgbox('调用成功',array('list'=>[]),2000);

    }
    private function _check_sign()
    {

    }

    /**
     *
     * @param  $latitude    纬度
     * @param  $longitude    经度
     * @param  $raidus        半径范围(单位：米)
     * @return multitype:number
     */
    public function getAround($latitude,$longitude,$raidus){
        $PI = 3.14159265;
        $degree = (24901*1609)/360.0;
        $dpmLat = 1/$degree;
        $radiusLat = $dpmLat*$raidus;
        $minLat = $latitude - $radiusLat;
        $maxLat = $latitude + $radiusLat;
        $mpdLng = $degree*cos($latitude * ($PI/180));
        $dpmLng = 1 / $mpdLng;
        $radiusLng = $dpmLng*$raidus;
        $minLng = $longitude - $radiusLng;
        $maxLng = $longitude + $radiusLng;
        return array (minLat=>$minLat, maxLat=>$maxLat, minLng=>$minLng, maxLng=>$maxLng);
    }
}